import time
import argparse
from generator import load_from_file
from alg import insertion_sort


def test_sorting_algorithm(algorithm, data, name):
    """测试排序算法"""
    test_data = data.copy()
    start_time = time.perf_counter()
    sorted_data = algorithm(test_data)
    end_time = time.perf_counter()
    is_correct = all(sorted_data[i] <= sorted_data[i + 1] for i in range(len(sorted_data) - 1))
    print(f"{name}: {'Correct' if is_correct else 'Incorrect'}")
    print(f"Time taken: {end_time - start_time:.6f} seconds")
    print(f"First 10 elements: {sorted_data[:10]}")
    print("-" * 50)


def main():
    parser = argparse.ArgumentParser(description='Test sorting algorithms')
    parser.add_argument('--data', type=str, required=True, help='Input data file')
    args = parser.parse_args()

    data = load_from_file(args.data)
    print(f"Loaded {len(data)} elements from {args.data}")

    test_sorting_algorithm(insertion_sort, data, "Insertion Sort")


if __name__ == "__main__":
    main()